package com.corex.vinayak;

import java.text.DateFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.classapp.schedule.Schedule;
import com.corex.responsebean.ScheduleResponse;
import com.corex.utils.Constants;
import com.corex.webservice.ScheduleService;

import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class TimeTableDetailsActivity extends Fragment {
	TextView timetableMonth;
	TextView timetableDate;

	LinearLayout timetableEventLayout;
	LinearLayout linearLayout;
	TextView timetableTime;
	TextView timetableEvent;
    TextView batchName;

	List classNameList;
	List classIdList;
    List batchNameList;
    List batchIdList;
	ProgressBar timetableSpinner;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		final View rootView = inflater.inflate(R.layout.timetable_date_details,
				container, false);

		/*
		 * String selectedDate = (String) getIntent().getExtras().get(
		 * Constants.TIMETABLE_SELECTED_DATE);
		 */

		String selectedDate = getArguments().getString(
				Constants.TIMETABLE_SELECTED_DATE);// "2014-11-02";

		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = dateFormat.parse(selectedDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// dateFormat = new SimpleDateFormat("yyyy-mmm-dd");
		final String dateFormated = dateFormat.format(date);

		DateFormatSymbols dateFormatSymbols = new DateFormatSymbols();
		String months[] = dateFormatSymbols.getMonths();
		/* Initialization */
		timetableMonth = (TextView) rootView.findViewById(R.id.timetable_month);
		timetableDate = (TextView) rootView.findViewById(R.id.timetable_date);
		timetableMonth.setText(months[date.getMonth()]);
		timetableDate.setText(date.getDate() + "");
		timetableEventLayout = (LinearLayout) rootView
				.findViewById(R.id.timetable_events);
		timetableSpinner = (ProgressBar) rootView
				.findViewById(R.id.timetableSpinner);

		final int length = 100;
		Thread t = new Thread(new Runnable() {

			@Override
			public void run() {
				ScheduleService scheduleService = new ScheduleService();
				final ScheduleResponse scheduleResponse = scheduleService
						.executeScheduleService(dateFormated);

				getActivity().runOnUiThread(new Runnable() {

					@Override
					public void run() {
						List<Schedule> schedules = new ArrayList();

						if (null != scheduleResponse
								&& null != scheduleResponse.getScheduleBean()
								&& null != scheduleResponse.getScheduleBean()
										.getSchedule()) {
							schedules = scheduleResponse.getScheduleBean()
									.getSchedule();
						}

						classNameList = new ArrayList();
						classIdList = new ArrayList();

						if (null != scheduleResponse
								&& null != scheduleResponse.getScheduleBean()
										.getStudentScheduleData()) {
							HashMap<String, List> hashMap = scheduleResponse
									.getScheduleBean().getStudentScheduleData();
							List clslist = hashMap.get(Constants.CLASS_LIST);

							if (null != clslist) {
								for (int i = 0; i < clslist.size(); i++) {
									List clslistInner = (List) clslist.get(i);
									classNameList.add(clslistInner.get(0));
									classIdList.add(clslistInner.get(1));

								}
							}
						}

                        batchNameList = new ArrayList();
                        batchIdList = new ArrayList();

                        if (null != scheduleResponse
                                && null != scheduleResponse.getScheduleBean()
                                .getStudentScheduleData()) {
                            HashMap<String, List> hashMap = scheduleResponse
                                    .getScheduleBean().getStudentScheduleData();
                            List bthlist = hashMap.get(Constants.BATCH_LIST);

                            if (null != bthlist) {
                                for (int i = 0; i < bthlist.size(); i++) {
                                    List bthlistInner = (List) bthlist.get(i);
                                    batchNameList.add(bthlistInner.get(0));
                                    batchIdList.add(bthlistInner.get(1));

                                }
                            }
                        }

                        System.out.println(batchIdList);
                        System.out.println(batchNameList);

						for (Schedule schedule : schedules) {
							timetableTime = new TextView(getActivity());
							linearLayout = new LinearLayout(getActivity());
							timetableEvent = new TextView(getActivity());
                            batchName = new TextView(getActivity());

                                    timetableTime.setText(schedule.getStart_time()
									+ " to " + schedule.getEnd_time() + " ");
							timetableTime.setTextColor(Color.RED);

							int indexOfId = classIdList
									.indexOf((double) schedule.getClass_id());
							timetableEvent.setText(" "
									+ classNameList.get(indexOfId).toString());

                            int indexOfBatchName = batchIdList.indexOf((double)schedule.getBatch_id());
                            batchName.setText("-"+batchNameList.get(indexOfBatchName).toString());

							linearLayout.addView(timetableTime);
							linearLayout.addView(timetableEvent);
                            linearLayout.addView(batchName);

							timetableEventLayout.addView(linearLayout);

						}

						if (null == schedules || schedules.size() == 0) {
							timetableTime = new TextView(getActivity());
							linearLayout = new LinearLayout(getActivity());
							timetableTime.setText("No Schedule");
							timetableTime.setTextColor(Color.RED);
							linearLayout.addView(timetableTime);
							timetableEventLayout.addView(linearLayout);

						}

						timetableEventLayout.removeView(timetableSpinner);
					}

				});

			}

		});
		t.start();

		return rootView;
	}
}
